﻿using EventBus;
using EventBus.Abstractions;
using EventBusRabbitMQ;
using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace ServiceCollection.Customization
{
    /// <summary>
    /// Service collection extension.
    /// </summary>
    public static class ServiceCollectionExt
    {
        public static IServiceCollection AddEventBus(this IServiceCollection services)
        {
            services.AddSingleton<IEventHandlerExecutionContext>(new EventHandlerExecutionContext(services));
            return services;
        }

        public static IServiceCollection AddRabbitMQ(this IServiceCollection services, string host, string userName, string password)
        {
            var factory = new ConnectionFactory()
            {
                HostName = host,
                UserName = userName,
                Password=password
            };
            services.AddSingleton<IEventBus>(sp => new EventBusRabbitMQ.EventBusRabbitMQ(factory,"exchange","direct","queue",true,sp.GetRequiredService<IEventHandlerExecutionContext>()));

            return services;
        }
    }
}
